package com.resou.reader.search.adpter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.api.entry.SearchBean;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.bookshelf.listener.OnItemClickListener;

import java.util.List;

/**
 * Created by ${Garrett} on 2018/9/6.
 * Contact me krouky@outlook.com
 */
public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder> {

    private List<SearchBean> mList;
    private String sourceText;
    private Context mContext;
    public SearchResultAdapter(Context context) {
        mContext = context;
    }

    public void setList(List<SearchBean> list, String sourceText) {
        mList = list;
        this.sourceText = sourceText;
    }

    private static final String TAG = "SearchResultAdapter-App";

    public void addList(List<SearchBean> list, String sourceText) {
        int previousItemCount = getItemCount();
        if (list != null && list.size() > 0) {
            mList.addAll(list);
//            notifyDataSetChanged();
            notifyItemRangeInserted(previousItemCount, list.size());
            this.sourceText = sourceText;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_item_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        SearchBean bean = mList.get(i);
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_RGB_565);
        Glide.with(viewHolder.itemView).load(bean.getCoverUrl()).apply(options).into(viewHolder.bookPicImage);

        spanString(viewHolder.bookNameText, bean.getNovelName());
        spanString(viewHolder.bookAuthor, bean.getAuthorName());

        viewHolder.bookDescText.setText(bean.getContent());
        viewHolder.labelState.setText(bean.getJoin());
        viewHolder.labelWordNum.setText(String.valueOf(bean.getBookPursuitVolume()));
        viewHolder.labelCategoryParent.setText(bean.getParentCategoryName());
        viewHolder.labelCategory.setText(bean.getCategoryName());
        viewHolder.itemView.setOnClickListener(v -> {
            BookDetailActivity.startDetailActivity(mContext, bean.getId());
        });
    }

    private void spanString(TextView textView, String text) {
        SpannableStringBuilder sbb = new SpannableStringBuilder(text);
        for (int i = 0; i < text.length(); i++) {
            String temp = text.substring(i, i + 1);
            if (sourceText.contains(temp)) {
                sbb.setSpan(new ForegroundColorSpan(textView.getResources().getColor(R.color.btn_login_highlight)), i, i + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);//Color.parseColor("#ffc36a")
            }
        }
        textView.setText(sbb);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //    mList == null ? 0 :
    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView bookPicImage;
        TextView bookNameText;
        TextView bookAuthor;
        TextView bookDescText;
        TextView labelState;
        TextView labelWordNum;
        TextView labelCategoryParent;
        TextView labelCategory;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            bookPicImage = itemView.findViewById(R.id.book_pic_image);
            bookNameText = itemView.findViewById(R.id.book_name_text);
            bookAuthor = itemView.findViewById(R.id.author_name_text);
            bookDescText = itemView.findViewById(R.id.book_desc_text);
            labelState = itemView.findViewById(R.id.label_state);
            labelWordNum = itemView.findViewById(R.id.label_word_num);
            labelCategoryParent = itemView.findViewById(R.id.label_category_parent);
            labelCategory = itemView.findViewById(R.id.label_category);

        }
    }
}
